﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Common;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Controls;
using SharpDiary.Core.Utility;

namespace SharpDiary.Diary
{
    [GuiModule("3b42c11a-18e8-476c-b6c3-87137d093c67", "DiaryManager", "Ray Shaw(demogodyou@gmail.com)")]
    public partial class frmDiaryManager : GuiModuleUIBase
    {
        #region Fields & Properties

        private int _matchedCount = 0;

        #endregion

        #region Construtors

        public frmDiaryManager()
        {
            InitializeComponent();
        }


        #endregion

        #region Methods

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (!DesignMode)
            {
                diaryInfoControlList.ShowItems();
                UpdateUI();
            }
        }


        private void UpdateUI()
        {
            toolStripStatusLabel.Text = string.Format("共 {0} 篇日记", DiaryInfo.GetTotalDiaryCount());
        }

        /// <summary>
        /// 处理快捷键。
        /// </summary>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.N: //Ctrl + N
                        {
                            //新日记
                            OwnerShell.ExecuteAction("SharpDiary.Diary", "c67c1ee9-8029-4849-8918-6cf3153497fe");

                            break;
                        }
                    case Keys.F:
                        {
                            pnlTop.Show();
                            break;
                        }
                }
            }
            else
            {
                switch (e.KeyCode)
                {
                    case Keys.F5: //刷新显示
                        {
                            RefreshData();
                            break;
                        }
                }
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            return base.ProcessDialogKey(keyData);
        }

        public override string GetSettingsKeyName()
        {
            return this.Name;
        }

        public override void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            base.OnMessageReceived(sender, e);

            if (e.TargetModuleId.Equals("3b42c11a-18e8-476c-b6c3-87137d093c67", StringComparison.CurrentCultureIgnoreCase))
            {
                switch (e.MessageInfo)
                {
                    case MessageDefinition.Refresh: //刷新显示
                        {
                            RefreshData();

                            break;
                        }
                }
            }
        }

        /// <summary>
        /// 刷新显示。
        /// </summary>
        private void RefreshData()
        {
            diaryInfoControlList.ShowItems();
            UpdateUI();
            pageButtonGroup.RefreshButtonsEnableState();
        }

        #endregion

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnNewEmotion_Click(object sender, EventArgs e)
        {
            //新日记
            OwnerShell.ExecuteAction("SharpDiary.Diary", "c67c1ee9-8029-4849-8918-6cf3153497fe");
        }

        private void pageButtonGroup_PageNumberChanged(object sender, PageNumberChangedEventArgs e)
        {
            //lblPageNumber.Text = string.Format("第{0}页/共{1}页", e.CurrentPageNumber, pageButtonGroup.PageCount);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 搜索。
        /// </summary>
        /// <param name="keywords"></param>
        private void Search(string keywords)
        {
            _matchedCount = 0;
            if (string.IsNullOrEmpty(keywords)) { return; }

            _matchedCount = diaryInfoControlList.Search(keywords);
            pageButtonGroup.RefreshButtonsEnableState();
            lblSearchResult.Text = (_matchedCount <= 0 ? "没有找到。" : string.Format("找到 {0} 篇日记", _matchedCount));
        }

        /// <summary>
        /// 重置搜索。
        /// </summary>
        private void ResetSearch()
        {
            _matchedCount = 0;
            lblSearchResult.Text = string.Empty;
            diaryInfoControlList.ShowItems();
            pageButtonGroup.RefreshButtonsEnableState();
        }

        private void searchTextBox_Search(object sender, OnSearchEventArgs e)
        {
            Search(e.KeyWord);
        }

        private void searchTextBox_ResetSearch(object sender, EventArgs e)
        {
            ResetSearch();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            pnlTop.Hide();
            lblSearchResult.Text = string.Empty;
            searchTextBox.KeyWord = string.Empty;

            Application.DoEvents();

            if (!pnlTop.Visible && diaryInfoControlList.IsSearchResult)
            {
                diaryInfoControlList.ShowItems();
            }
        }

        private void pnlTop_VisibleChanged(object sender, EventArgs e)
        {

        }
    }
}
